package com.gjun.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gjun.struts.bean.GjunsystemPrivilege;
import com.gjun.struts.bean.GjunsystemUser;
import com.gjun.struts.resource.AdminDAOIf;
import com.gjun.struts.resource.GjunPrivilegeResourceIf;
import com.gjun.struts.resource.GjunSystemUserResourceIf;
import com.gjun.struts.util.MD5;

public class AdminLoginAction {
	private String username;

	private String passwd;

	private String verifycode; // 验证码
	
	//private AdminDAOIf adminDAO;    // 最后spring注入给我们的是代理对象
	
	private GjunPrivilegeResourceIf privilegeDAO;
	private GjunSystemUserResourceIf systemUserDAO;
	
	
	
	
	private String result;
	
	
	
	

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	

	

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getVerifycode() {
		return verifycode;
	}

	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}
	
	public GjunSystemUserResourceIf getSystemUserDAO() {
		return systemUserDAO;
	}

	public void setSystemUserDAO(GjunSystemUserResourceIf systemUserDAO) {
		this.systemUserDAO = systemUserDAO;
	}

	public GjunPrivilegeResourceIf getPrivilegeDAO() {
		return privilegeDAO;
	}

	public void setPrivilegeDAO(GjunPrivilegeResourceIf privilegeDAO) {
		this.privilegeDAO = privilegeDAO;
	}
	
	
	public String execute() throws Exception{
		//System.out.println("usernameddddddddddddd:  "+username);
		//System.out.println("adminDAO==============>    "+systemUserDAO);
		
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		
		String loginCode = (String)session.getAttribute("rand");
		if(loginCode==null || !loginCode.equals(verifycode)){
			result = "codeFaild";
			return "success";
		}
		
		GjunsystemUser user = systemUserDAO.loadLoginAdmin(username, MD5.toMD5(passwd));
		
		//System.out.println("user:  "+user);
		if(user!=null){
			//System.out.println(user.getCode()+"11111111111111");
			if(user.getState()==null || !user.getState().equals(1) ){ 
				//System.out.println("stateFaild/////////////////////asdasdasd");
				result = "stateFaild";
				
			}else{
				//System.out.println("true/////////////////////asdasdasdasd");
				result = "true";
			    
			    session.setAttribute("account",username);
			    session.setAttribute("loginTimer",new Date());
			    session.setAttribute("accountId",user.getUserid());
			    session.setAttribute("loginAccount", user);
			    System.out.println(user.getEmail());
			    List<GjunsystemPrivilege> allPris = privilegeDAO.loadAllPrivilege();
			    session.setAttribute("allPris",allPris);
			    
			    List<GjunsystemPrivilege> accountPris = systemUserDAO.loadUserPrivilegesByUserId(user.getUserid());
			    session.setAttribute("accountPris",accountPris);
			}
		}else{
			result = "false";
		}
		//System.out.println("result:   "+result);
		return "success";
	}

	

	

}
